package com._51cto.healthy.service;

import com._51cto.healthy.base.BaseMapper;
import com._51cto.healthy.content.P;
import com._51cto.healthy.mapper.MemberMapper;
import com._51cto.healthy.pojo.Member;
import com._51cto.healthy.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.*;

@DubboService
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public BaseMapper<Member> getDao() {
        return memberMapper;
    }


    @Override
    public P<Member> queryMember(String query, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Member> members = memberMapper.queryMember(query);
        PageInfo<Member> memberPageInfo = new PageInfo<>(members);
        return new P()
                .setPageNum(memberPageInfo.getPageNum())
                .setPageSize(memberPageInfo.getPageSize())
                .setPages(memberPageInfo.getPages())
                .setRows(memberPageInfo.getList())
                .setTotal(memberPageInfo.getTotal());
    }

    @Override
    public Map<String, List<String>> getMemberReportForPastSixMonth() {
        Map<String, List<String>> result = new HashMap<>();
        memberMapper.selectMemberReportForPastSixMonth()
                .entrySet()
                .forEach(entity -> {
                    result.put(entity.getKey(), Arrays.asList(entity.getValue().split(",")));
                });
        return result;
    }

//    客户端登录
    @Override
    public Member clientLogin(String phoneNumber, String password) {
        Member member = memberMapper.clientLogin(phoneNumber, password);
        return member;
    }

    @Override
    public Member findUserByName(String phoneNumber) {
        Member byName = null;
        boolean a = false;
        while(!a){
            try{
                byName = memberMapper.findUserByName(phoneNumber);
                a = true;
            }catch (RuntimeException e){
                a = false;
            }
        }
        return  byName;
    }

    @Override
    public int clientRegister(Member member) {
        Date date = new Date();
        Date nowDate = null;
        try {
            nowDate = DateUtils.getDate(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        member.setRegTime(nowDate);
        return memberMapper.clientRegister(member);
    }

    @Override
    public Member findUserById(Integer id) {
        return memberMapper.findUserById(id);
    }

    @Override
    public int updateMemberById(Member member) {
        return memberMapper.updateMemberById(member);
    }

    //    会员添加
    @Override
    public void insert(Member member) {
        memberMapper.insert(member);
    }

    @Override
    public void update(Member member) {
        memberMapper.update(member);
    }
}
